#pragma once

#include "Comm.hpp"
#include "Log.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream> //文件操作

const std::string gdictanme = "dict.txt";
const std::string gdictpath = "./";
const std::string gsep = ": ";

using namespace lyrics;
class Dict
{
private:
    bool LoadDict()
    {
        std::string file = _path + _filename;
        std::ifstream in(file.c_str());
        if (!in.is_open())
        {
            LOG(LogLevel::ERROR) << "open file " << file << "error";
            return false;
        }

        std::string line;
        while (std::getline(in, line))
        {
            // 切成左右两部分
            std::string key;
            std::string value;
            if (splitString(line, &key, &value, gsep))
            {
                // 成功之后插入到字典当中
                _dict[key] = value;
            }
        }
        Print();
        in.close();
        return true;
    }

public:
    Dict(const std::string &path = gdictpath, const std::string &filename = gdictanme)
        : _path(path), _filename(filename)
    {
        LoadDict();//初始化字典
    }
    std::string Translate(const std::string &word)
    {
        auto iter = _dict.find(word);
        if(iter == _dict.end()) 
        {
            return "None";
        }
        return iter->second;
    }
    void Print()
    {
        for(auto iter :_dict)
        {
            std::cout << iter.first << ":" << iter.second << std::endl;
        }
    }
    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict; // 字典
    std::string _path;                                  // 字典的路径
    std::string _filename;
    ; // 字典的名字
};